using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace NoteManger
{
	public class SqlServerDBObject : DataBaseObject
	{
		protected SqlConnection aSqlServerConnection = new SqlConnection();
		public override bool Connect(string mdbFilePath)
		{
			this._Message = "派生类不支持该重载版本的函数。";
			this._Connected = false;
			return false;
		}
		public override bool Connect(string aServerName, string aUserName, string aPassword, string aDataBaseName)
		{
			bool result;
			try
			{
				string connectionString = string.Concat(new string[]
				{
					"server=",
					aServerName,
					";uid=",
					aUserName,
					";pwd=",
					aPassword,
					";database=",
					aDataBaseName
				});
				this.aSqlServerConnection.Close();
				this.aSqlServerConnection.ConnectionString = connectionString;
				this.aSqlServerConnection.Open();
				this._Connected = true;
				result = true;
			}
			catch (Exception ex)
			{
				this._Message = ex.Message;
				this._Connected = false;
				result = false;
			}
			return result;
		}
		public override bool Connect(string aDataSource, string aUserName, string aPassword)
		{
			this._Message = "派生类不支持该重载版本的函数。";
			this._Connected = false;
			return false;
		}
		public override void DisConnect()
		{
			try
			{
				this.aSqlServerConnection.Close();
			}
			catch (Exception ex)
			{
				this._Message = ex.Message;
			}
		}
		public override string[,] Open(string aStrSql)
		{
			this._Message = "";
			string[,] array = new string[0, 0];
			string[,] result;
			if (!this._Connected)
			{
				this._Message = "数据库尚未连接！";
				result = array;
			}
			else
			{
				SqlCommand sqlCommand = new SqlCommand();
				try
				{
					sqlCommand.Connection = this.aSqlServerConnection;
					sqlCommand.CommandText = aStrSql;
					SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
					if (sqlDataReader.HasRows)
					{
						int num = 0;
						while (sqlDataReader.Read())
						{
							for (int i = 0; i <= sqlDataReader.FieldCount - 1; i++)
							{
								array = (string[,])DataBaseObject.ChangeArrayLength(array, num + 1, sqlDataReader.FieldCount);
								array[num, i] = sqlDataReader.GetValue(i).ToString();
							}
							num++;
						}
						sqlDataReader.Close();
						sqlDataReader.Dispose();
						sqlCommand.Dispose();
						result = array;
					}
					else
					{
						sqlDataReader.Close();
						sqlDataReader.Dispose();
						sqlCommand.Dispose();
						result = array;
					}
				}
				catch (Exception ex)
				{
					this._Message = ex.Message.ToString();
					sqlCommand.Dispose();
					result = array;
				}
			}
			return result;
		}
		public override void Open(string aStrSql, DataGridView objDataGridView)
		{
			this._Message = "";
			SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
			SqlCommand sqlCommand = new SqlCommand();
			DataSet dataSet = new DataSet();
			try
			{
				sqlCommand.Connection = this.aSqlServerConnection;
				sqlCommand.CommandText = aStrSql;
				sqlDataAdapter.SelectCommand = sqlCommand;
				dataSet.Clear();
				sqlDataAdapter.Fill(dataSet, "table");
				objDataGridView.DataSource = dataSet.Tables[0].DefaultView;
			}
			catch (Exception ex)
			{
				this._Message = ex.Message.ToString();
			}
			finally
			{
				sqlDataAdapter.Dispose();
				sqlCommand.Dispose();
				dataSet.Dispose();
			}
		}
		public override int ExecSQL(string aStrSql)
		{
			this._Message = "";
			int result;
			if (!this._Connected)
			{
				this._Message = "数据库尚未连接！";
				result = -1;
			}
			else
			{
				SqlCommand sqlCommand = new SqlCommand();
				try
				{
					sqlCommand.Connection = this.aSqlServerConnection;
					sqlCommand.CommandText = aStrSql;
					int num = sqlCommand.ExecuteNonQuery();
					result = num;
				}
				catch (Exception ex)
				{
					this._Message = ex.Message.ToString();
					result = -1;
				}
				finally
				{
					sqlCommand.Dispose();
				}
			}
			return result;
		}
	}
}
